package com.wsoft.manager;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsoft.constant.RedisKeyConstant;
import com.wsoft.core.annotation.GetRedisCache;
import com.wsoft.core.service.RedisService;
import com.wsoft.entity.TsJobEntity;
import com.wsoft.mapper.TsJobMapper;
import com.wsoft.query.job.JobListQuery;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author z001
 * @Date 2024/10/23 09:08
 */
@Service
public class TsJobManager extends ServiceImpl<TsJobMapper, TsJobEntity> {

    @Resource
    private RedisService redisService;

    /**
     * 根据code查询
     * @param code
     * @return
     */
    @GetRedisCache( type = RedisKeyConstant.Job.JOB_CODE,
            expire_sec = RedisKeyConstant.Job.EXPIRED,
            value = RedisKeyConstant.REDIS_SELF)
    public TsJobEntity getByCode(String code){
        if(StrUtil.isBlank(code)){
            return null;
        }
        QueryWrapper<TsJobEntity> query = new QueryWrapper<>();
        query.lambda().eq(TsJobEntity::getCode,code);
        return getOne(query);
    }


    /**
     * 根据id修改
     * @param entity
     * @return
     */
    @Override
    public boolean updateById(TsJobEntity entity){
        boolean b = super.updateById(entity);
        if(b){
            redisService.del(RedisKeyConstant.Job.JOB_CODE + entity.getCode());
            redisService.del(RedisKeyConstant.Job.JOB_ID + entity.getId());
        }
        return b;
    }

    /**
     * 根据id 获取
     * @param id
     * @return
     */
    @GetRedisCache( type = RedisKeyConstant.Job.JOB_ID,
            expire_sec = RedisKeyConstant.Job.EXPIRED,
            value = RedisKeyConstant.REDIS_SELF)
    public TsJobEntity getById(Long id){
       return super.getById(id);
    }


    /**
     * 根据id删除
     * @param id
     * @return
     */
    public boolean removeById(Long id){
        TsJobEntity entity = this.getById(id);
        boolean b = super.removeById(id);
        if(b){
            redisService.del(RedisKeyConstant.Job.JOB_CODE + entity.getCode());
            redisService.del(RedisKeyConstant.Job.JOB_ID + entity.getId());
        }
        return b;
    }

    /**
     * 查询列表
     * @param query
     * @return
     */
    public List<TsJobEntity> listJob(JobListQuery param) {
        QueryWrapper<TsJobEntity> query = new QueryWrapper<>();
        query.lambda().eq(StrUtil.isNotBlank(param.getCode()),TsJobEntity::getCode,param.getCode())
                .eq(StrUtil.isNotBlank(param.getName()),TsJobEntity::getName,param.getName())
                .eq(ObjUtil.isNotEmpty(param.getStatus()),TsJobEntity::getStatus,param.getStatus());
        return list(query);
    }
}
